package com.android.dialer.extendboard;

import com.android.dialer.common.LogUtil;
import org.greenrobot.eventbus.*;

public class EventBusRegisterHelper {
    private static final String TAG = "EventBusRegisterHelper";
    private boolean isRegistered = false;
    private Object target;

    public EventBusRegisterHelper() {
        super();
    }

    public void register(Object object) {
        LogUtil.i(TAG, "register: target is null ? " + (target == null));
        LogUtil.i(TAG, "register: isRegistered ? " + isRegistered);

        if (object == null) {
            return;
        }
        if (isRegistered) {
            return;
        }

        target = object;
        isRegistered = true;
        EventBus.getDefault().register(object);
    }

    public void unregister(Object object) {
        LogUtil.i(TAG, "unregister: target is null ? " + (target == null));
        LogUtil.i(TAG, "unregister: isRegistered ? " + isRegistered);

        if (object == null) {
            return;
        }

        if (!isRegistered) {
            return;
        }

        EventBus.getDefault().unregister(object);
        isRegistered = false;
        target = null;
    }
}
